import { $ } from './library/jquery-module.js';
import './library/jquery-extend-slider.js';
import './library/jquery-lazyload-min.js';
import cookie from './library/cookie.js';


$(function() {
    $('#slider').slider();

    $.ajax({
        async: false,
        type: "get",
        url: "../../interface/index.php",
        dataType: "json",
        success: function(res) {
            let temp = '';
            res.forEach(function(elm, _index) {
                let pics = JSON.parse(elm.picture);
                let goodstitle = JSON.parse(elm.title);

                temp += `<a href="./goods.html?id=${elm.id}">
                        <img src="../${pics[0].src}" alt="">
                        <span></span>
                        <p>${goodstitle[0].name.slice(0,20)+'...'}</p>
                        <div>￥${elm.price}</div>
                    </a>`;

                // 懒加载
                $('img.lazy').lazyload({
                    placeholder: '../img/lazy-img.png',
                    effect: 'fadeIn'
                });
            });

            $('.internationgoods>.othergoods').html(temp);
        }
    });

    // 导航

    $('#nav-list>li>a').on('click', function() {
        let elm = $(`#${$(this).attr('title')}`);
        let elmtop = elm.offset().top - 50;

        $('html').animate({
            scrollTop: elmtop
        }, 1000, function() {
            console.log('end')
        });
    });
    $(window).on('scroll', function() {
        let dctop = $(document).scrollTop();
        // console.log(dctop);

        if (dctop < 660) {
            $('#nav-list').css({
                'width': '0',
                'height': '0',
                'opacity': '0'
            });

            $('#index-search').slideUp(500);

            $('#index-search>input').css('display', 'none');
            $('#index-search>button').css('display', 'none');

        } else if (dctop >= 660) {
            $('#nav-list').css({
                'width': '36px',
                'height': '200px',
                'opacity': '1'
            });

            $('#index-search').slideDown(500);
            $('#index-search>input').css('display', 'block');
            $('#index-search>button').css('display', 'block');
        }

        if (dctop > 1063 && dctop < 1685) {
            $('#nav-list>li>a').removeClass('active').eq(1).addClass('active');

        } else if (dctop > 1685 && dctop < 2400) {
            $('#nav-list>li>a').removeClass('active').eq(2).addClass('active');
        } else if (dctop > 2400) {
            $('#nav-list>li>a').removeClass('active').eq(3).addClass('active');

        }
    });

    // 用户成功登录
    if (cookie.get('username') && cookie.get('isLogined')) {
        $('.fir-left').children('a').eq(0).text('Hi , ' + cookie.get('username'))
        $('.fir-left').children('a').eq(1).css('display', 'none');
        $('.fir-left').children('span').css('display', 'none');
    }
});